package A_review.review9_15;

import binaryTree.basic.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Cengxu {
    //TODO:2023/9/15 通过的
    class Solution {
        public List<List<Integer>> resList = new ArrayList<List<Integer>>();
        public List<List<Integer>> levelOrder(TreeNode node) {
            if (node == null) return resList;
            Queue<TreeNode> que = new LinkedList<TreeNode>();
            que.offer(node);
            while (!que.isEmpty()) {
                List<Integer> itemList = new ArrayList<Integer>();
                int len = que.size();
                while (len > 0) {
                    TreeNode tmpNode = que.poll();
                    itemList.add(tmpNode.val);
                    if (tmpNode.left != null) que.offer(tmpNode.left);
                    if (tmpNode.right != null) que.offer(tmpNode.right);
                    len--;
                }
                resList.add(itemList);
            }
            return resList;
        }
    }

        //DFS--递归方式
        public void checkFun01(TreeNode node) {
            List<List<Integer>>  res=new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            if (node==null){
                return;
        }
            queue.offer(node);
            //TODO:2023/9/15  这里队列忘记加上！

            while (!queue.isEmpty()){
                int size = queue.size();
                ArrayList<Integer> temp = new ArrayList<>();
                while (size>0){
                    TreeNode cur = queue.poll();
                    if (cur.left!=null){
                        queue.offer(cur.left);
                    }
                    if (cur.right!=null){
                        queue.offer(cur.right);
                    }
                    temp.add(cur.val);
                    //TODO:2023/9/15  忘记了size--

                    size--;
                }
                res.add(temp);
            }
    }
}
